package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 检查组管理
 */
@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;
    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        log.debug("CheckGroupController-add: CheckGroup{ " + checkGroup + " }");
        log.debug("CheckGroupController-add: checkitemIds{ " + Arrays.toString(checkitemIds) + " }");
        checkGroupService.add(checkGroup,checkitemIds);
        log.debug(MessageConst.ADD_CHECKGROUP_SUCCESS);
        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        log.debug("CheckGroupController-findPage: QueryPageBean{ " + pageBean + " }");
        PageResult pageResult = checkGroupService.findPage(pageBean);
        log.debug("CheckGroupController-findPage: PageResult {" + pageResult + " }");
        return pageResult;
    }

    //根据id查询检查组
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("CheckGroupController-findById: id{ " + id + " }");
        CheckGroup checkGroup = checkGroupService.findById(id);
        log.debug("CheckGroupController-findById: checkGroup{ " + checkGroup + " }");
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    //根据检查组id查询关联的检查项id
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer checkgroupId){
        log.debug("CheckItemController-findCheckItemIdsByCheckGroupId: checkgroupId: { " + checkgroupId + " }");
        List<Integer> list = checkGroupService.findCheckItemIdsByCheckGroupId(checkgroupId);
        log.debug("checkItemIdList: { " + list + " }");
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,list);
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        log.debug("CheckGroupController-edit: CheckGroup{ " + checkGroup + " }");
        log.debug("CheckGroupController-edit: checkitemIds{ " + Arrays.toString(checkitemIds) + " }");
        checkGroupService.edit(checkGroup,checkitemIds);
        log.debug(MessageConst.EDIT_CHECKGROUP_SUCCESS);
        return new Result(true, MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/delById")
    public Result delById(Integer id){
        log.debug("delById : { " + id +"}" );
        checkGroupService.delById(id);
        log.debug(MessageConst.DELETE_CHECKGROUP_SUCCESS);
        return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        log.debug("CheckGroupController:findAll: " );
        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        log.debug("检查组查询成功！！！");
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
    }
}
